package space.sakana.edusysback.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import space.sakana.edusysback.result.Result;
import space.sakana.edusysback.service.JobService;

@RestController
@RequestMapping("/api/job")
public class JobController {

    private JobService jobService;

    @Autowired
    public void autowiredService(JobService jobService){
        this.jobService = jobService;
    }

    /**
     * 通过jid获取job
     * @param jid job id
     */
    @RequestMapping("/{jid}")
    public Result getJob(@PathVariable int jid, HttpServletRequest request, HttpServletResponse response){
        return jobService.getJob(jid, request, response);
    }

    @RequestMapping("/delete/{jid}")
    public Result deleteJob(@PathVariable int jid, HttpServletRequest request, HttpServletResponse response){
        return jobService.deleteJob(jid, request, response);
    }

    /**
     * 添加job
     * @param data job json
     */
    @RequestMapping("/add")
    public Result addJob(@RequestParam String data, HttpServletRequest request, HttpServletResponse response){
        return jobService.addJob(data, request, response);
    }

    /**
     * 修改job
     * @param data job json
     */
    @RequestMapping("/update")
    public Result updateJob(@RequestParam String data, HttpServletRequest request, HttpServletResponse response){
        System.out.println(data);
        return jobService.updateJob(data, request, response);
    }

    /**
     * 获取job内的job item
     * @param jid job id
     */
    @RequestMapping("/item/{jid}")
    public Result getJobItems(@PathVariable int jid, HttpServletRequest request, HttpServletResponse response){
        return jobService.getJobItems(jid, request, response);
    }

    /**
     * 向job内添加job item
     * @param jid job id
     * @param data job item json
     */
    @RequestMapping("/item/add/{jid}")
    public Result addJobItem(@PathVariable int jid, @RequestParam String data, HttpServletRequest request, HttpServletResponse response){
        return jobService.addJobItem(jid, data, request, response);
    }

    @RequestMapping("/all")
    public Result getAllJob(HttpServletRequest request, HttpServletResponse response){
        return jobService.getAllJob(request, response);
    }

    /**
     * 修改job item
     * @param jid job id
     * @param data job item json
     */
    @RequestMapping("/item/update/{jid}")
    public Result updateJobItem(@PathVariable int jid, @RequestParam String data, HttpServletRequest request, HttpServletResponse response){
        return jobService.updateJobItem(jid, data, request, response);
    }

    @RequestMapping("/target/add")
    public Result addJobTarget(@RequestParam int uid, @RequestParam int jid, HttpServletRequest request, HttpServletResponse response){
        return jobService.addJobTarget(uid, jid, request, response);
    }

    @RequestMapping("/push")
    public Result pushJob(@RequestParam int jid, HttpServletRequest request, HttpServletResponse response){
        return jobService.pushJob(jid, request, response);
    }

    @RequestMapping("/push/item")
    public Result pushJobItem(@RequestParam int jid, @RequestParam int pid,
                              @RequestParam int qid, @RequestParam String answer,
                              HttpServletRequest request, HttpServletResponse response){
        return jobService.pushJobItem(jid, pid, qid, answer, request, response);
    }

    @RequestMapping("/wait")
    public Result waitJob(HttpServletRequest request, HttpServletResponse response){
        return jobService.getWaitJob(request, response);
    }

    @RequestMapping("/all/finish")
    public Result getAllFinishJob(HttpServletRequest request, HttpServletResponse response){
        return jobService.getAllFinishJob(request, response);
    }

    @RequestMapping("/push/item/{id}")
    public Result getPushItem(@PathVariable int id, HttpServletRequest request, HttpServletResponse response){
        return jobService.getPushItem(id, request, response);
    }

    @RequestMapping("/push/item/edit")
    public Result editPushItem(@RequestParam int id, @RequestParam int qid, @RequestParam int score, HttpServletRequest request, HttpServletResponse response){
        return jobService.editPushItem(id, qid, score, request, response);
    }

    @RequestMapping("/finish/{id}")
    public Result jobFinish(@PathVariable int id, HttpServletRequest request, HttpServletResponse response){
        return jobService.jobFinish(id, request, response);
    }

}
